﻿using System;
using System.ComponentModel;

namespace HalconDotNet
{
	/// <summary>Represents an instance of a file.</summary>
	// Token: 0x0200003B RID: 59
	public class HFile : HHandle
	{
		// Token: 0x06000554 RID: 1364 RVA: 0x0000883B File Offset: 0x00006A3B
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HFile() : base(HHandleBase.UNDEF)
		{
		}

		// Token: 0x06000555 RID: 1365 RVA: 0x0001EC62 File Offset: 0x0001CE62
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HFile(IntPtr handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x06000556 RID: 1366 RVA: 0x0001EC71 File Offset: 0x0001CE71
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HFile(HHandle handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x06000557 RID: 1367 RVA: 0x0001EC80 File Offset: 0x0001CE80
		private void AssertSemType()
		{
			base.AssertSemType("file");
		}

		// Token: 0x06000558 RID: 1368 RVA: 0x0001EC8D File Offset: 0x0001CE8D
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HFile obj)
		{
			obj = new HFile(HHandleBase.UNDEF);
			return obj.Load(proc, parIndex, err);
		}

		// Token: 0x06000559 RID: 1369 RVA: 0x0001ECA8 File Offset: 0x0001CEA8
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HFile[] obj)
		{
			HTuple htuple;
			err = HTuple.LoadNew(proc, parIndex, err, out htuple);
			obj = new HFile[htuple.Length];
			for (int i = 0; i < htuple.Length; i++)
			{
				obj[i] = new HFile(HalconAPI.IsLegacyHandleMode() ? htuple[i].IP : htuple[i].H);
			}
			htuple.Dispose();
			return err;
		}

		/// <summary>
		///   Open a file in text or binary format.
		///   Modified instance represents: File handle.
		/// </summary>
		/// <param name="fileName">Name of file to be opened. Default: "standard"</param>
		/// <param name="fileType">Type of file access and optional the string encoding. Default: "output"</param>
		// Token: 0x0600055A RID: 1370 RVA: 0x0001ED18 File Offset: 0x0001CF18
		public HFile(string fileName, HTuple fileType)
		{
			IntPtr proc = HalconAPI.PreCall(1659);
			HalconAPI.StoreS(proc, 0, fileName);
			HalconAPI.Store(proc, 1, fileType);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(fileType);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Open a file in text or binary format.
		///   Modified instance represents: File handle.
		/// </summary>
		/// <param name="fileName">Name of file to be opened. Default: "standard"</param>
		/// <param name="fileType">Type of file access and optional the string encoding. Default: "output"</param>
		// Token: 0x0600055B RID: 1371 RVA: 0x0001ED74 File Offset: 0x0001CF74
		public HFile(string fileName, string fileType)
		{
			IntPtr proc = HalconAPI.PreCall(1659);
			HalconAPI.StoreS(proc, 0, fileName);
			HalconAPI.StoreS(proc, 1, fileType);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Open a file in text or binary format.
		///   Modified instance represents: File handle.
		/// </summary>
		/// <param name="fileName">Name of file to be opened. Default: "standard"</param>
		/// <param name="fileType">Type of file access and optional the string encoding. Default: "output"</param>
		// Token: 0x0600055C RID: 1372 RVA: 0x0001EDC8 File Offset: 0x0001CFC8
		public void OpenFile(string fileName, HTuple fileType)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(1659);
			HalconAPI.StoreS(proc, 0, fileName);
			HalconAPI.Store(proc, 1, fileType);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(fileType);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Open a file in text or binary format.
		///   Modified instance represents: File handle.
		/// </summary>
		/// <param name="fileName">Name of file to be opened. Default: "standard"</param>
		/// <param name="fileType">Type of file access and optional the string encoding. Default: "output"</param>
		// Token: 0x0600055D RID: 1373 RVA: 0x0001EE24 File Offset: 0x0001D024
		public void OpenFile(string fileName, string fileType)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(1659);
			HalconAPI.StoreS(proc, 0, fileName);
			HalconAPI.StoreS(proc, 1, fileType);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Write strings and numbers into a text file.
		///   Instance represents: File handle.
		/// </summary>
		/// <param name="stringVal">Values to be written into the file. Default: "hallo"</param>
		// Token: 0x0600055E RID: 1374 RVA: 0x0001EE78 File Offset: 0x0001D078
		public void FwriteString(HTuple stringVal)
		{
			IntPtr proc = HalconAPI.PreCall(1660);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, stringVal);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(stringVal);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Write strings and numbers into a text file.
		///   Instance represents: File handle.
		/// </summary>
		/// <param name="stringVal">Values to be written into the file. Default: "hallo"</param>
		// Token: 0x0600055F RID: 1375 RVA: 0x0001EEBC File Offset: 0x0001D0BC
		public void FwriteString(string stringVal)
		{
			IntPtr proc = HalconAPI.PreCall(1660);
			base.Store(proc, 0);
			HalconAPI.StoreS(proc, 1, stringVal);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Read a character line from a text file.
		///   Instance represents: File handle.
		/// </summary>
		/// <param name="isEOF">Reached end of file before any character was read.</param>
		/// <returns>Read line.</returns>
		// Token: 0x06000560 RID: 1376 RVA: 0x0001EEF8 File Offset: 0x0001D0F8
		public string FreadLine(out int isEOF)
		{
			IntPtr proc = HalconAPI.PreCall(1661);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			HalconAPI.InitOCT(proc, 1);
			int num = HalconAPI.CallProcedure(proc);
			string result;
			num = HalconAPI.LoadS(proc, 0, num, out result);
			num = HalconAPI.LoadI(proc, 1, num, out isEOF);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Read a string from a text file.
		///   Instance represents: File handle.
		/// </summary>
		/// <param name="isEOF">Reached end of file before any character was added to the output string.</param>
		/// <returns>Read character sequence.</returns>
		// Token: 0x06000561 RID: 1377 RVA: 0x0001EF54 File Offset: 0x0001D154
		public string FreadString(out int isEOF)
		{
			IntPtr proc = HalconAPI.PreCall(1662);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			HalconAPI.InitOCT(proc, 1);
			int num = HalconAPI.CallProcedure(proc);
			string result;
			num = HalconAPI.LoadS(proc, 0, num, out result);
			num = HalconAPI.LoadI(proc, 1, num, out isEOF);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Read one character from a text file.
		///   Instance represents: File handle.
		/// </summary>
		/// <returns>Read character, which can be multi-byte or the control string 'eof'.</returns>
		// Token: 0x06000562 RID: 1378 RVA: 0x0001EFB0 File Offset: 0x0001D1B0
		public string FreadChar()
		{
			IntPtr proc = HalconAPI.PreCall(1663);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			string result;
			num = HalconAPI.LoadS(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Write a line break and clear the output buffer.
		///   Instance represents: File handle.
		/// </summary>
		// Token: 0x06000563 RID: 1379 RVA: 0x0001EFF8 File Offset: 0x0001D1F8
		public void FnewLine()
		{
			IntPtr proc = HalconAPI.PreCall(1664);
			base.Store(proc, 0);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>Closing a text file.</summary>
		/// <param name="fileHandle">File handle.</param>
		// Token: 0x06000564 RID: 1380 RVA: 0x0001F02C File Offset: 0x0001D22C
		public static void CloseFile(HFile[] fileHandle)
		{
			HTuple htuple = HHandleBase.ConcatArray(fileHandle);
			IntPtr proc = HalconAPI.PreCall(1665);
			HalconAPI.Store(proc, 0, htuple);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(htuple);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(fileHandle);
		}

		/// <summary>
		///   Closing a text file.
		///   Instance represents: File handle.
		/// </summary>
		// Token: 0x06000565 RID: 1381 RVA: 0x0001F070 File Offset: 0x0001D270
		public void CloseFile()
		{
			IntPtr proc = HalconAPI.PreCall(1665);
			base.Store(proc, 0);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Read bytes from a binary file.
		///   Instance represents: File handle.
		/// </summary>
		/// <param name="numberOfBytes">Number of bytes to be read.</param>
		/// <param name="isEOF">Indicates if end of file is reached while reading  the file.</param>
		/// <returns>Bytes read from the input binary file.</returns>
		// Token: 0x06000566 RID: 1382 RVA: 0x0001F0A4 File Offset: 0x0001D2A4
		public HTuple FreadBytes(int numberOfBytes, out int isEOF)
		{
			IntPtr proc = HalconAPI.PreCall(2182);
			base.Store(proc, 0);
			HalconAPI.StoreI(proc, 1, numberOfBytes);
			HalconAPI.InitOCT(proc, 0);
			HalconAPI.InitOCT(proc, 1);
			int num = HalconAPI.CallProcedure(proc);
			HTuple result;
			num = HTuple.LoadNew(proc, 0, HTupleType.INTEGER, num, out result);
			num = HalconAPI.LoadI(proc, 1, num, out isEOF);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Write bytes to a binary file.
		///   Instance represents: File handle.
		/// </summary>
		/// <param name="dataToWrite">Data to be written to the file.</param>
		/// <returns>Number of bytes written to the output  binary file.</returns>
		// Token: 0x06000567 RID: 1383 RVA: 0x0001F108 File Offset: 0x0001D308
		public int FwriteBytes(HTuple dataToWrite)
		{
			IntPtr proc = HalconAPI.PreCall(2183);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, dataToWrite);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(dataToWrite);
			int result;
			num = HalconAPI.LoadI(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}
	}
}
